package com.shiftychess.model
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.data.DataService;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.RTMPChannel;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class ConnectionManager extends EventDispatcher
	{
		private static var conn:ConnectionManager;
		
		private var requestDS:DataService;
		private var playerDS:DataService;
		private var gameDS:DataService;
		private var moveDS:DataService;
		
		private var player:Player;
		
		private var userName:String;
		
		private var online:Boolean;
		
		private var requestlist:ArrayCollection;
		private var gamelist:ArrayCollection;
		private var movelist:ArrayCollection;
		
		public function ConnectionManager() {
			requestDS = new DataService("request");
			playerDS = new DataService("player");
			gameDS = new DataService("game");
			moveDS = new DataService("move");
			
			requestDS.autoCommit = false;
			playerDS.autoCommit = false;
			gameDS.autoCommit = false;
			moveDS.autoCommit = false;
			
			player = new Player();
			
			var channelset:ChannelSet = new ChannelSet();
			channelset.addChannel(new RTMPChannel("my-rtmp", "rtmp://10.1.1.10:2038"));
			
			gameDS.channelSet = channelset;
			playerDS.channelSet = channelset;
			moveDS.channelSet = channelset;
			requestDS.channelSet = channelset;
			
			//var game:Game = new Game();
			
			gamelist = new ArrayCollection();
			//gameDS.fill(gamelist, "by-player", player.playerId);
			gameDS.fill(gamelist);
		}
		
		public static function getInstance():ConnectionManager {
			if(conn == null) {
				conn = new ConnectionManager();
			}
			return conn;
		}
		
		public function getUserName():String {
			return userName;
		}
		
		public function setUserName(name:String):void {
			userName = userName;
			player.name = userName;
		}
		
		public function loadRequestList():void {
			requestDS.fill(requestlist);
			requestDS.addEventListener(ResultEvent.RESULT, requestsLoaded);
		}
			
		private function requestsLoaded(event:ResultEvent):void {
			//TODO create players file
		}
		
		public function requestGame():void {
			requestlist.addItem(userName);
		}
		
		public function startGame():void {
			player.inGame = true;
			player.name = "Jeroen";
			player.playerId = 2;

			var player2:Player = new Player();
			//TODO
			player2.name = "Guy from Clipboard"
			player2.inGame = true;
			player2.playerId = 3;
			
			var game:Game = new Game();
			game.gameId = 1;
			game.player1 = player;
			game.player2 = player2;
			gamelist.addItem(game);
			gameDS.commit();
			gameDS.addEventListener(ResultEvent.RESULT, gameChanged);
		}
		
		private function getGame():Game {
			return gamelist.getItemAt(0) as Game;
		}		
		
		public function getGameId():int {
			var game:Game = gamelist.getItemAt(0) as Game;
			if(game != null) {
				return game.gameId;
			} else {
				return -1;
			}
		}
		
		private var firsttime:Boolean = true;
		
		private function gameChanged(event:ResultEvent):void {
			//init moves
			if(movelist == null) {
				movelist = new ArrayCollection();
				//moveDS.fill(movelist, "by-game", getGame().gameId);
				//moveDS.fill(movelist);				
			}
			
			//game changed, update pieces
			
			if(!firsttime) {
				GameModel.pieces = getGame().pieces;
				dispatchEvent(new Event("updatePieces"));				
			}
			firsttime = false;
			
			trace("hey loaded");
			
		}
		
		public function sendMove(move:Move):void {
			if(!getGame().ended) {
				move.movingPlayer = player;
				move.game = getGame();
				move.moveNumber = movelist.getItemAt(movelist.length).moveNumber + 1;
				movelist.addItem(move);
				getGame().pieces = GameModel.pieces;
			}
		}
		
		public function endGame(winner:String):void {
			//send message
			//TODO
			//end game
			getGame().ended = true;			
		}
		
		public function replay():void {
			//execute all moves from the move list
			//movelist
		}
		
		public function print():void {
			var xml:XML = <body></body>;
			
			for(var i:int; i < movelist.length; i++) {
				var move:Move = movelist.getItemAt(i) as Move;
				
				xml.appendChild(<move>
									<moveFrom>move.from</moveFrom>
									<moveTo>move.to</moveTo>
									<capture>move.capture</capture>
								</move>);
					
			}

			var printserver:RemoteObject = new RemoteObject();
			printserver.destination = "PDFService";

			
		}
		
	}
}